package LeetCode.NumOffer;

/**
 * 从尾到头打印链表
 * @author lumin
 * @date 2022/03/17 16:46
 **/
public class NumOffer06 {
    public class ListNode {
       int val;
       ListNode next;
       ListNode(int x) { val = x; }
   }
    public int[] reversePrint(ListNode head) {
        if(head == null){
            int nums[] = new int[0];
            return nums;
        }
        int lengthNode = 0;
        for (ListNode i = head; i != null ; i = i.next) {
            lengthNode++;
        }
        int arr[] = new int[lengthNode];
        if(head.next == null){
            arr[arr.length - 1] = head.val;
        }
        ListNode prev = head;
        int length = arr.length - 1;
        for (ListNode i = head; i != null ; i = i.next) {
            arr[length] = i.val;
            length--;
        }
        return arr;
    }
}